
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Xml;


using Swaf.BizObj;

namespace Swaf
{
	public class SerializationInfo
	{
		public SerializationInfo(string typeName, string serialObject)
		{
			TypeName = typeName;
			SerialObject = serialObject;
		}
		public string TypeName = null;
		public string SerialObject = null;
	}

	/// <summary>
	/// Summary description for Serialization.
	/// </summary>
	public class Serialization
	{
		public static SerializationInfo serialize(object obj)
		{
			if (obj == null)
				return new SerializationInfo("NULL", "");
			if (obj is IBizObj)
				return serialize((IBizObj)obj);
			else if (obj is IBizObjCursor)
				return serialize((IBizObjCursor)obj);
			else if (obj is IBizObjList)
				return serialize((IBizObjList)obj);
			else if (obj is XmlDocument)
				return serialize((XmlDocument)obj);
			else if (obj is XmlElement)
				return serialize((XmlElement)obj);
			else
				return new SerializationInfo(obj.GetType().FullName, obj.ToString());
		}

		public static SerializationInfo serialize(object obj, string extraInfo)
		{
			if (obj == null)
				return new SerializationInfo("NULL", "");
			if(obj is IBizObj)
				return serialize((IBizObj)obj, extraInfo);
			else if(obj is IBizObjCursor)
				return serialize((IBizObjCursor)obj);
			else if(obj is IBizObjList)
				return serialize((IBizObjList)obj, extraInfo);
			else if(obj is XmlDocument)
				return serialize((XmlDocument)obj);
			else if(obj is XmlElement)
				return serialize((XmlElement)obj);
			else
				return new SerializationInfo(obj.GetType().FullName, obj.ToString());
		}

		public static SerializationInfo serialize(IBizObj obj)
		{
			return new SerializationInfo("IBizObj." + obj.type.Name, obj.getXml("setOnly basicProperties objProperties listProperties sysProperties"));
		}

		public static SerializationInfo serialize(IBizObj obj, string extraInfo)
		{
			return new SerializationInfo("IBizObj." + obj.type.Name, obj.getXml(String.Format("setOnly deco({0}) basicProperties objProperties listProperties sysProperties", extraInfo)));
		}


		public static SerializationInfo serialize(IBizObjCursor obj)
		{
			return new SerializationInfo("IBizObjCursor." + obj.Source.objectType.Name, obj.Source.getXml("setOnly basicProperties objProperties listProperties sysProperties"));
		}

		public static SerializationInfo serialize(IBizObjList obj)
		{
			return new SerializationInfo("IBizObjList." + obj.objectType.Name, obj.getXml("setOnly basicProperties objProperties listProperties sysProperties"));
		}

		public static SerializationInfo serialize(IBizObjList obj, string extraInfo)
		{
			return new SerializationInfo("IBizObjList." + obj.objectType.Name, obj.getXml(String.Format("setOnly deco({0}) basicProperties objProperties listProperties sysProperties", extraInfo)));
		}

		public static SerializationInfo serialize(XmlDocument obj)
		{
			return new SerializationInfo(obj.GetType().FullName, obj.OuterXml);
		}

		public static SerializationInfo serialize(XmlElement obj)
		{
			return new SerializationInfo(obj.GetType().FullName, obj.OuterXml);
		}

		public static object deserialize(string type, string data)
		{
			return deserialize(type, data, null);
		}
		public static object deserialize(string type, string data, string extraInfo)
		{
			IApplication app = Application.currentApp;
			string dataType = type;
			string secondaryDataType = null;
			object deserializedObject = data;

			if (type == null)
				return data;
			if(type == "NULL")
				return null;
			if(!NameAddress.isSingleAddress(dataType))
			{
				secondaryDataType = NameAddress.getRemainingAddress(dataType);
				dataType = NameAddress.getFirstAddress(dataType);
			}

			if(dataType.Equals("IBizObj") || dataType.Equals("IBizObjList"))
			{
				IBizObjSource bizSrc = app.bizObjMgr.createSource(SourceType.BasicSource, data, extraInfo);
				if(dataType.Equals("IBizObj"))
					deserializedObject = bizSrc.getList(secondaryDataType, null)[0];
				else
					deserializedObject = bizSrc.getList(secondaryDataType, null);
			}
			else if(type.Equals("System.Xml.XmlDocument") || type.Equals("System.Xml.XmlElement"))
			{
				XmlDocument doc = new XmlDocument();
				doc.LoadXml(data);
				if(type.Equals("System.Xml.XmlDocument"))
					return doc;
				else
					return doc.DocumentElement;
			}
			else
			{
				if(type.Equals("System.String") || type.Equals("String"))
					deserializedObject = data;
				else
				{
					Type[] paramType = new Type[]{typeof(String)};
					object[] param = new object[]{data};
					try
					{
						deserializedObject = Cmn.callCtor(type, paramType, param);
					}
					catch(Exception e)
					{
						try
						{
							deserializedObject = Cmn.callStatic(type, "Parse", paramType, param);
						}
						catch(Exception)
						{
							deserializedObject = null;
						}
					}
				}
			}
			try
			{
				if(deserializedObject == null)
					deserializedObject = app.factories["BizObjBizFieldTypes"].createFromNickName(type, new Object[]{data});
			}
			catch(Exception)
			{
				deserializedObject = null;
			}

			return deserializedObject;
		}
	}
}
